/*
 * 文  件   名： YearMonthDayTimeDialog.java
 * 版          权： Copyright GoPawPaw All Rights Reserved.
 * 描          述：[该类的简要描述]
 * 创  建   人： LiJinHua
 * 创建时间： 2012-6-4
 * 
 * 修   改  人：
 * 修改时间：
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import app.utils.CommonUtils;
import app.widget.BaseDialog;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author LiJinHua
 * @date 2012-6-4
 * @version [Android 1.0.0, 2012-6-4]
 * @description
 */
public class TimeDialog extends BaseDialog implements
		OnWheelChangedListener, OnWheelScrollListener {

	/**
	 * 返回的结果小时
	 */
	private int mHour = 0;
	/**
	 * 返回结果的分钟
	 */
	private int mMinu = 0;
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 小时滚动View
	 */
	private WheelView hour;
	/**
	 * 分钟滚动View
	 */
	private WheelView minu;

	/**
	 * 日历
	 */
	private Calendar calendar;
	/**
	 * 返回view
	 */
	private View contentView;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param title
	 *            标题
	 */
	public TimeDialog(Context context, String title) {

		super(context);
		this.context = context;
		setTitle(title);
		initDate();

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化数据
	 */
	private void initDate() {
		calendar = CommonUtils.getCurrentTimeByCalendar();
		// 初始化View
		initView();
		// 添加监听器
		CalendarDialogCommon.addChangeListener(this, minu, hour);
		
		setBtn1ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (onYearMonthDayAmPmDialogScrollEndListener != null) {
					onYearMonthDayAmPmDialogScrollEndListener
							.onYearMonthDayTimeDialogScrollEndListener(mHour
									+ "", CalendarDialogCommon
									.convertZeroString(mMinu));
				}

				TimeDialog.this.dismiss();
			}
		});

		setBtn2ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				TimeDialog.this.dismiss();
			}
		});

		mHour = calendar.get(Calendar.HOUR_OF_DAY);
		hour.setViewAdapter(new DateNumericAdapter(context, 0,
				23, mHour, "时"));
		hour.setCurrentItem(mHour);
		// month
		mMinu = calendar.get(Calendar.MINUTE);
		minu.setViewAdapter(new DateNumericAdapter(context, 0,
				59, mMinu, "分"));
		minu.setCurrentItem(mMinu);
		
	}
	
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param y 年
	 * @param m 月
	 * @param d 日
	 * @param am 上午下午
	 */
	 public void select(int h, int m) {
		 
		 hour.setCurrentItem(h);
		 minu.setCurrentItem(m);
		 hour.invalidate();
		 minu.invalidate();
		 
   }

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化view
	 */
	private void initView() {

		contentView = LayoutInflater.from(getContext()).inflate(
				R.layout.time_layout, null);

		hour = (WheelView) contentView.findViewById(R.id.hour);
		minu = (WheelView) contentView.findViewById(R.id.minu);
		minu.addScrollingListener(this);
		hour.addScrollingListener(this);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 滑动结束监听器
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2011-12-30]
	 */
	public interface OnYearMonthDayTimeDialogScrollEndListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * @param hour
		 * @param minu
		 */
		void onYearMonthDayTimeDialogScrollEndListener(String hour,
				String minu);
	}

	/**
	 * 滑动结束监听器
	 */
	private OnYearMonthDayTimeDialogScrollEndListener onYearMonthDayAmPmDialogScrollEndListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnYearMonthDayAmPmDialogScrollEndListener
	 */
	public OnYearMonthDayTimeDialogScrollEndListener getOnYearMonthDayAmPmDialogScrollEndListener() {
		return onYearMonthDayAmPmDialogScrollEndListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onYearMonthDayAmPmDialogScrollEndListener
	 */
	public void setOnYearMonthDayTimeDialogScrollEndListener(
			OnYearMonthDayTimeDialogScrollEndListener onYearMonthDayAmPmDialogScrollEndListener) {
		this.onYearMonthDayAmPmDialogScrollEndListener = onYearMonthDayAmPmDialogScrollEndListener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {

		switch (wheel.getId()) {
		case R.id.hour:
			mHour = newValue;
			break;
		case R.id.minu:
			mMinu = newValue;
			break;
		default:
			break;
		}
	}

	@Override
	public View createContentView() {

		return contentView;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
//		if (onYearMonthDayAmPmDialogScrollEndListener != null) {
//			onYearMonthDayAmPmDialogScrollEndListener.onScrollEnd(mHour + "",
//					CalendarDialogCommon.convertZeroString(mMinu), false);
//		}

		setBtn1Enable(false);
		setBtn2Enable(false);
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
//		if (onYearMonthDayAmPmDialogScrollEndListener != null) {
//			onYearMonthDayAmPmDialogScrollEndListener.onScrollEnd(mHour + "",
//					CalendarDialogCommon.convertZeroString(mMinu),true);
//		}
		setBtn1Enable(true);
		setBtn2Enable(true);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void yearScroll(int position) {
		minu.scroll(position, 0);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param position
	 *            滚动的位置
	 */
	public void monthScroll(int position) {
		hour.scroll(position, 0);
	}

}
